<template>
  <div>
    <h5 class="text-muted">当前任务：贷后管理</h5>
    <el-form class="query-form" @submit.native.prevent :inline="true">
      <el-form-item>
        <select-address
          v-model="params.address_code"
          auto-top
          clearable
          placeholder="所属地"
          @change="getData()"
        ></select-address>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.customer_name"
          :clearable="true"
          placeholder="主体名称查询"
          @change="getData()"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.lender_idcard"
          :clearable="true"
          placeholder="身份证号码"
          @change="getData()"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>

      <el-form-item class="float-right">
        <el-button
          type="primary"
          icon="el-icon-download"
          :loading="exportLoading"
          @click="handleDownload()"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      class="table-sm"
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
    >
      <template slot="load_status" slot-scope="scope">
        <el-tag :type="scope.row.loadStyle">{{ scope.row.loadStatus }}</el-tag>
      </template>

      <template slot="leftFunds" slot-scope="scope">
        <span v-if="+scope.row.load_beh_status === 6">
          {{ $utils.dateFormat(scope.row.load_date, 'yyyy-MM-dd') }}
        </span>
        <span v-else>{{ scope.row.left_funds }}</span>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="reportVisible"
      :close-on-click-modal="false"
      :before-close="handleCreditClose"
      top="30px"
      width="80%"
    >
      <credit-file :query-id="queryId"></credit-file>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import SelectAddress from '@/components/select/SelectAddress'
import { enumRenewalStatus } from '@/credit/plugins/enum'
import { enumProjectType } from '@/gov/plugins/enum'
import CreditFile from '@/gov/components/creditFile/GovCredit'

export default {
  components: {
    SelectAddress,
    CreditFile,
  },
  props: {},
  data() {
    return {
      tableName: 'customer_related',
      tableId: '',
      params: {
        index: 1,
        size: 15,
        customer_name: '',
        lender_idcard: '',
        address_code: '',
        'loan_notice_status.eq': 2,
      },
      loading: false,

      tableData: [],
      tableCount: 0,
      tableColumns: [
        {
          attrs: { label: '项目名称', prop: 'customer_name', minWidth: '160px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '地址', prop: 'address', minWidth: '160px' } },
        {
          attrs: { label: '项目类型', prop: 'project_type', align: 'center' },
          process: v => enumProjectType[v],
        },
        { attrs: { label: '负责人', prop: 'corp_name' } },
        { attrs: { label: '联系电话', prop: 'mobile_no' } },
        { attrs: { label: '放款金额（万元）', prop: 'reply_funds', align: 'center' } },
        { attrs: { label: '在保余额（万元）', align: 'center' }, slot: 'leftFunds' },
        {
          attrs: { label: '到期日期', prop: 'load_end_date', align: 'center' },
          process: v => this.$utils.dateFormat(v, 'yyyy-MM-dd'),
        },
        { attrs: { label: '贷后状态', align: 'center' }, slot: 'load_status' },
        {
          attrs: { label: '操作', align: 'center' },
          operations: [
            { content: '贷后管理', icon: 'fa fa-edit', event: this.handleToLoan },
            { content: '查看', icon: 'fa fa-file', event: this.handleView },
          ],
        },
      ],
      enumRenewalStyle: {
        0: 'primary',
        1: 'success',
        2: 'info',
        3: 'warning',
        4: 'warning',
        5: 'danger',
        6: 'success',
      },

      queryId: '',
      reportVisible: false,
      exportLoading: false,
      submit_type: '',
    }
  },
  computed: {
    ...mapGetters(['org']),
  },
  watch: {
    $route(route, oldroute) {
      if (route.name === 'missionNormal') {
        if (oldroute.name === 'missionNormal') {
          this.params.index = 1
          this.params.size = 15
        }
        this.getData()
      }
    },
  },
  created() {
    this.params.address_code = this.$utils.getAddressCode()
    let orgClass = this.org.orgClass
    this.submit_type = orgClass === 'org_gov_ny' ? '1' : '4'
    this.params['submit_type.eq'] = this.submit_type
    this.getData()
  },
  mounted() {},
  methods: {
    async getData(index = 1) {
      this.params.index = index
      const params = { ...this.params }
      const status = this.$route.query.status
      if (status !== undefined) {
        if (+status === 1) {
          params['load_beh_status.eq'] = status
        } else if (+status === 2) {
          params['load_beh_status.in'] = '2, 3, 4, 5'
        } else {
          params['load_beh_status.eq'] = status
        }
      }
      if (+status === 6) {
        this.$set(this.tableColumns[6].attrs, 'label', '贷款日期')
        this.$set(this.tableColumns[7].attrs, 'label', '完结日期')
      } else {
        this.$set(this.tableColumns[6].attrs, 'label', '在保余额（万元）')
        this.$set(this.tableColumns[7].attrs, 'label', '到期日期')
      }
      this.loading = true
      const [res] = await this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, params)
      )
      this.loading = false

      const data = (res && res.data) || {}
      this.tableData = (data.data || []).map(item => {
        // load_beh_status  贷后状态 1 正常 2 关注 3 次级 4 可疑 5 损失 6已完结
        item.loadStatus = enumRenewalStatus[item.load_beh_status] || '-'
        item.loadStyle = this.enumRenewalStyle[item.load_beh_status] || ''
        return item
      })
      this.tableCount = data.totalCount || 0
    },
    handleView(row) {
      this.queryId = row.customer_related_id
      this.reportVisible = true
    },
    handleToLoan(row) {
      this.$router.push({
        name: 'postloan_list',
        query: {
          id: row.customer_related_id,
          farmid: row.customer_id,
        },
      })
    },
    async handleDownload() {
      const { index, size, ...params } = this.params // eslint-disable-line
      const exportParams = {
        service: this.$serviceName,
        table: this.tableName,
        params: params,
        filename: '贷后管理.xlsx',
      }

      this.exportLoading = true
      await this.$utils.promise(this.$api.exportData(exportParams))
      this.exportLoading = false
    },
    handleCreditClose() {
      this.reportVisible = false
      this.getData(this.params.index)
    },
  },
}
</script>

<style lang="scss" scoped></style>
